package org.wzys.Service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.wzys.Mapper.CarItemMapper;
import org.wzys.Mapper.OrderMapper;
import org.wzys.Mapper.ProductMapper;
import org.wzys.Pojo.*;
import org.wzys.Service.CaritemService;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class CaritemServiceimpl implements CaritemService {


    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private CarItemMapper caritemMapper;



    @Transactional
    @Override
    public void createOrder( List<Integer> productIds,Integer userId)
            throws Exception{
        Order order = new Order();
        order.setOrderId(String.valueOf(System.currentTimeMillis()));
        order.setCreatedTime(java.time.LocalDateTime.now());
        order.setUserId(userId);
        order.setStatus(0); // 假设0表示待支付
        orderMapper.insertOrder(order);
        List<Integer> quantities=new ArrayList<>();
        for(Integer productId:productIds){
            quantities.add(caritemMapper.getQuantityByProductId(productId));
        }
        // 遍历商品ID列表，添加到订单
        BigDecimal totalAmount =BigDecimal.ZERO;
        for (int i = 0; i < productIds.size(); i++) {
            Integer productId = productIds.get(i);
            Integer quantity = quantities.get(i);
            Product product = productMapper.selectByPrimaryKey(productId);
            BigDecimal quantity1= new BigDecimal(quantity);
            if (product.getQuantity()>= quantity) {
                // 更新库存
                productMapper.updateStock(productId, product.getQuantity() - quantity);

                // 创建订单项
                OrderItem item = new OrderItem();
                item.setOrderId(order.getOrderId());
                item.setProductId(productId);
                item.setProductName(product.getName());
                item.setPrice(product.getPrice());
                item.setQuantity(quantity);
                item.setTotalPrice(quantity1.multiply(product.getPrice()));
                orderMapper.insertOrderItem(item);

                totalAmount =totalAmount.add(item.getTotalPrice());

            } else {
                // 处理库存不足的情况
              throw new RuntimeException("库存不足");

            }
        }
        // 计算订单总金额
        String a=order.getOrderId();
        orderMapper.insertprise(a,totalAmount);
    }

    @Override
    public PageResult getCaritempage(ProductQueryparm parm) {
        //实现分页查询
        //1. 设置PageHelper分页参数
        PageHelper.startPage(parm.getPagenum(), parm.getPageSize());
        //2. 执行查询
        List<Caritem> ProductList =caritemMapper.list(parm);
        //3. 封装分页结果
        Page<Caritem> p = (Page<Caritem>) ProductList;
        return new PageResult(p.getTotal(), p.getResult());
       
    }

    @Override
    public void updateOrder(Integer quantity,Integer productId) {
        LocalDateTime utime = LocalDateTime.now();
        caritemMapper.updateOrder(quantity,productId, utime);
    }

    @Override
    public void deleteCaritm(Integer productId) {
        caritemMapper.deleteCaritm(productId);

    }
}
